package com.boot.core.filter

import com.boot.config.RequestWrapper
import org.springframework.stereotype.Component
import java.io.IOException
import javax.servlet.*
import javax.servlet.annotation.WebFilter
import javax.servlet.http.HttpServletRequest

/**
 * 过滤器Filter, 用来把request传递下去
 */
@Component
@WebFilter(urlPatterns = ["/*"], filterName = "channelFilter")
class ChannelFilter : Filter {
    @Throws(ServletException::class)
    override fun init(filterConfig: FilterConfig) {
    }

    @Throws(IOException::class, ServletException::class)
    override fun doFilter(servletRequest: ServletRequest, servletResponse: ServletResponse, filterChain: FilterChain) {
        var requestWrapper: ServletRequest? = null
        if (servletRequest is HttpServletRequest) {
            requestWrapper = RequestWrapper(servletRequest)
        }
        if (requestWrapper == null) {
            filterChain.doFilter(servletRequest, servletResponse)
        } else {
            filterChain.doFilter(requestWrapper, servletResponse)
        }
    }

    override fun destroy() {}
}